<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue的$watch监听</title>
</head>
<body>
  <div id="app">
    <h2>{{info.name}}</h2>
    <button @click="changeInfo()">修改info</button>
  </div>
  <script src="../lib/vue.js"></script>
  <script>
    // 使用vue
    const app = Vue.createApp({
      data: function(){
        return {
          message: "hello watch",
          info: {name: "小米info", price: 3000},
        }
      }
      ,methods: {
        changeInfo() {
          // 1. 创建新对象赋值给info
          this.info = {name: "华为info", price: 3800}
        }
      },
      // 生命周期回调函数: 当前的组件创建时,自动执行
      // 一般在该函数中, 会进行网络请求
      created() {
        // 发送ajax/fetch/axios
        console.log("created..........")
        this.$watch("info", (newValue, oldValue) => {
          console.log("info数据发生变化: ", newValue, oldValue)
        }, {deep: true})
      },
      
    }
    )
    // 挂载 
    app.mount("#app")
  </script>

</body>
</html>
